package com.example.dao;

import com.example.bean.ProjectBean;
import com.example.bean.ReportBean;
import com.example.mapper.generator.ProjectPoMapper;
import com.example.mapper.generator.TeamProjectPoMapper;
import com.example.mapper.generator.po.ProjectPo;
import com.example.mapper.generator.po.ProjectPoExample;
import com.example.mapper.generator.po.TeamProjectPo;
import com.example.mapper.generator.po.TeamProjectPoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

@Repository
public class ProjectDao {
    @Autowired
    TeamProjectPoMapper teamProjectPoMapper;
    @Autowired
    ProjectPoMapper projectPoMapper;
    @Autowired
    TeamDao teamDao;
    @Autowired
    TeacherDao teacherDao;
    public List<Long> getProjectIdsByTeamId(Long teamId){
        TeamProjectPoExample example = new TeamProjectPoExample();
        TeamProjectPoExample.Criteria criteria = example.createCriteria();
        criteria.andTeamIdEqualTo(teamId);
        List<TeamProjectPo> pos = teamProjectPoMapper.selectByExample(example);
        return pos.stream().map(TeamProjectPo::getProjectId).toList();
    }

    public ProjectPo getProjectById(Long id){
        return projectPoMapper.selectByPrimaryKey(id);
    }

    public List<ReportBean> getReportsByTeamId(Long teamId) {
        TeamProjectPoExample example = new TeamProjectPoExample();
        TeamProjectPoExample.Criteria criteria = example.createCriteria();
        criteria.andTeamIdEqualTo(teamId);
        List<TeamProjectPo> pos = teamProjectPoMapper.selectByExample(example);
        return pos.stream().map(po -> {
            ReportBean report = ReportBean.builder().project_id(po.getProjectId()).content(po.getReport()).build();
            report.setProject_name(this.getProjectById(po.getProjectId()).getName());
            report.setTeam_name(teamDao.getTeamById(po.getTeamId()).getName());
            report.setTeacher_name(teacherDao.getTeacherById(po.getReviewTeacherId()).getName());
            report.setSubmit_status(!report.getContent().isEmpty());
            report.setReview_status(!po.getReviewContent().isEmpty() && null != po.getGrade());
            return report;
        }).toList();
    }

    public List<ProjectBean> getAllProjects() {
        return projectPoMapper.selectByExample(new ProjectPoExample()).stream().map(po -> {
            ProjectBean project = ProjectBean.builder().id(po.getId()).name(po.getName()).start(po.getStart()).end(po.getEnd()).description(po.getDescription()).build();
            project.setStatus(LocalDate.now().isAfter(po.getStart()) && LocalDate.now().isBefore(po.getEnd()));
            return project;
        }).toList();
    }
}
